import socket,time,select
from collections import deque
symmetric_side_port = 47420
peer_ip = '120.244.142.105'
add3 = (peer_ip, symmetric_side_port)   
import time
st = time.time()
q = deque([],400)
g_a = None
g_s = None

while True:
    if g_s:
        break

    for i in range(10000,20000):
        if i%1000==0:
            print(i,time.time()-st)
        time.sleep(0.001)
        add = ('0.0.0.0',i)
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)    
        s.bind(add)
        
        s.sendto(bytes(str(i),encoding='utf8'),add3)
        q.append(s)
        if i%100==0:
            r,w,e = select.select(list(q),[],[],0.001)
            if  r:
                g_s = r[0]
                g_a = s.getsockname()
                print(g_a)
                break
q = None

while True:
    time.sleep(1)
    g_s.sendto(bytes(str(time.time()),encoding='utf8'),add3)
    while True:
        r,w,e = select.select([g_s],[],[],0.001)
        if not r:
            break
        a = g_s.recvfrom(10000)
        print('got',a,g_s)


